<?php


namespace App\Service\Admin;


use App\Constants\Model\Notice;
use App\Dao\NoticeAdminDao;
use App\Model\NoticeAdmin;
use Hyperf\Contract\PaginatorInterface;
use Hyperf\Di\Annotation\Inject;

class NotificationService extends BaseService
{

    /**
     * @Inject()
     * @var NoticeAdminDao
     */
    protected $noticeAdminDao;

    /**
     * @param array $data
     * @return array
     */
    public function index(array $data)
    {
        $index = $this->noticeAdminDao->where([
            'admin_user_id' => $this->user()->id,
            'status' => Notice::STATUS_UNREAD
        ])->paginate($data['perPage'], ['*'], 'page', $data['page']);
        return $this->formatIndex($index);
    }

    /**
     * @param PaginatorInterface $paginator
     * @return array
     */
    public function formatIndex(PaginatorInterface $paginator)
    {
        $data = [];

        /** @var NoticeAdmin $item */
        foreach ($paginator->items() as $item) {
            $data[] = [
                'id' => $item->id,
                'type' => $item->type,
                'content' => $item->content,
                'extension' => $item->extension,
                'created_at' => $item->created_at->toDateTimeString()
            ];
        }

        return [
            'per_page' => $paginator->perPage(),
            'total' => $paginator->total(),
            'current_page' => $paginator->currentPage(),
            'data' => $data

        ];
    }

    public function read(array $ids)
    {
        $this->noticeAdminDao->where([
            'ids' => $ids
        ])->update(
            ['status' => Notice::STATUS_READ]
        );
    }

}